package com.system.manager.config;

import com.system.manager.common.helper.IRedisHelper;
import com.system.manager.common.interceptor.AccessInterceptor;
import com.system.manager.common.interceptor.ThreadLocalMapInterceptor;
import com.system.manager.common.interceptor.TokenInterceptor;
import com.system.manager.common.resolver.CurrSubjectResolver;
import com.system.manager.utils.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname SystemManagerWebMvcConfig
 * @Description TODO webMvc解析器
 * @Date 2021/10/17 18:44
 * @Created by 杨小鹏：【241960695@qq.com】
 */
@Configuration
public class SystemManagerWebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private IRedisHelper redisHelper;

    @Autowired
    private TokenManager tokenManager;

    @Bean
    TokenInterceptor tokenInterceptor() {
        return new TokenInterceptor(tokenManager);
    }

    @Bean
    AccessInterceptor accessInterceptor() {
        return new AccessInterceptor();
    }

    @Bean
    ThreadLocalMapInterceptor threadLocalMapInterceptor(){
        return new ThreadLocalMapInterceptor();
    }
    /**
     * 配置拦截器的拦截路径
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 1.添加拦截的名单
        List<String> blackPatterns = new ArrayList<>();
        blackPatterns.add("/**");

        // 2.添加白名单
        List<String> whitePatterns = new ArrayList<>();
        whitePatterns.add("/user/login");
        whitePatterns.add("/error");
        whitePatterns.add("/swagger-resources/**");
        whitePatterns.add("/webjars/**");
        whitePatterns.add("/v2/**");
        whitePatterns.add("/swagger-ui.html/**");
        whitePatterns.add("/csrf");
        whitePatterns.add("/");

        // 3.注册threadLocalMapInterceptor拦截器
        InterceptorRegistration threadLocalReg = registry.addInterceptor(threadLocalMapInterceptor());
        threadLocalReg.addPathPatterns(blackPatterns).// 添加黑名单
                excludePathPatterns(whitePatterns); // 添加白名单

        // 4.注册TokenInterceptor拦截器
        InterceptorRegistration tokenReg = registry.addInterceptor(tokenInterceptor());
        tokenReg.addPathPatterns(blackPatterns).// 添加黑名单
                excludePathPatterns(whitePatterns); // 添加白名单

        // 5.注册accessInterceptor拦截器
        InterceptorRegistration authorizedReg = registry.addInterceptor(accessInterceptor());
        authorizedReg.addPathPatterns(blackPatterns).// 添加黑名单
                excludePathPatterns(whitePatterns); // 添加白名单

    }

    /**
     * 添加注解解析器
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        // 将自己创建的注解解析器加入spring中
        resolvers.add(new CurrSubjectResolver());
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")
                .allowedMethods("*")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
        /*
         * 一小时内不需要再预检
         */
    }
}
